package  
{	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.events.Event;
	import flash.geom.Matrix;
	import flash.media.Camera;
	import flash.media.Video;
	import flash.display.LoaderInfo;
	import org.libspark.flartoolkit.core.raster.rgb.FLARRgbRaster_BitmapData;
	
	import org.libspark.flartoolkit.core.FLARCode;
	import org.libspark.flartoolkit.core.param.FLARParam;
	import org.libspark.flartoolkit.core.raster.FLARRgbRaster;
	import org.libspark.flartoolkit.core.transmat.FLARTransMatResult;
	import org.libspark.flartoolkit.detector.FLARSingleMarkerDetector;
	import org.libspark.flartoolkit.support.pv3d.FLARCamera3D;
	import org.libspark.flartoolkit.support.pv3d.FLARMarkerNode;
	import org.papervision3d.events.FileLoadEvent;
	import org.papervision3d.objects.parsers.KMZ;
	import org.papervision3d.scenes.Scene3D;
	import org.papervision3d.view.BasicView;
	import org.papervision3d.view.Viewport3D;

	
	[SWF (width="800", height="600", backgroundColor="0x000000",frameRate="30")]	
	
	
	public class RAInfo extends BasicView 
	{
		
		//[Embed(source="../marcas/patt.sample1", mimeType="application/octet-stream")]
		private var MarcaPadrao:Class;
		
		
		public var parametrosCam:FLARParam;
		public var marca:FLARCode;
		public var raster:FLARRgbRaster_BitmapData//novo
		public var detector:FLARSingleMarkerDetector;
		public var result:org.FLARTransMatResult
		public var flarCam:FLARCamera3D;
		public var container:FLARMarkerNode;
		
	
		public var video:Video;
		public var webCam:Camera;
		public var camBitmapData:BitmapData;
		public var camBitmap : Bitmap; 
		
			
		public const WIDTH:Number = 800;
		public const HEIGHT:Number = 600;
		
		public var threshold : int = 80; 
		public var thresholdVariance : int = 1; 
		public var currentThreshold : int; 
		public var counter : int = 0; 		
		private var model:KMZ;
		
		private var variavel:Object;
		private var modelo:String;
		
		
		public function RAInfo()
		{			
			super(800,600, false); 
			parametrosCam = FLARParam.getDefaultParam(WIDTH * 0.5, HEIGHT * 0.5);			
			marca = new FLARCode(16, 16);
			marca.loadARPattFromFile(new MarcaPadrao());			
			init();
		}
		
		public function init():void
		{
			video = new Video(WIDTH, HEIGHT);
			webCam = Camera.getCamera();
			webCam.setMode(WIDTH, HEIGHT, 30);
			video.attachCamera(webCam);
			video.smoothing = true; 			
			camBitmapData = new BitmapData(WIDTH * 0.5, HEIGHT * 0.5,false, 0x000000);			
			camBitmap = new Bitmap(camBitmapData); 
			camBitmap.scaleX = camBitmap.scaleY = 2; 
			addChildAt(camBitmap,0);			
			raster = new FLARRgbRaster(WIDTH *0.5, HEIGHT * 0.5);
			detector = new FLARSingleMarkerDetector(parametrosCam, marca, 100);
			result = new FLARTransMatResult();			
			viewport.x = 100;
			viewport.y = -100;
			_camera = new FLARCamera3D(parametrosCam);
			container = new FLARMarkerNode();
			scene.addChild(container);			
			addModelo(); 			
			stage.addEventListener(Event.ENTER_FRAME, enterFrame);
		}
		
		public function addModelo() : void
		{
			variavel=LoaderInfo(this.root.loaderInfo).parameters;
			if(variavel['modelo']==1){
				modelo="../KMZ/som.kmz";
			}	else if (variavel['modelo']==2){
				modelo="../KMZ/mb.kmz";
			}	else if (variavel['modelo']==3){
				modelo="../KMZ/proces.kmz";
			}   else if (variavel['modelo']==4){
				modelo="../KMZ/hd2.kmz";
			}	else if (variavel['modelo']==5){
				modelo="../KMZ/GABINETE.kmz";
			}			
			else{
				modelo="../KMZ/GABINETE.kmz";
			}						
			model = new KMZ();
			model.scale=1;
			model.load(modelo);			
		}
				
		public function enterFrame(e:Event):void
		{
			var scaleMatrix:Matrix = new Matrix();
			scaleMatrix.scale(0.5,0.5);
			camBitmapData.draw(video, scaleMatrix);			
			raster.setBitmapData(camBitmapData);			
			counter++; 						
			if(counter == 3) counter = 0; 			
			var imageFound : Boolean = false			
			currentThreshold = threshold+ (((counter%3)-1)*thresholdVariance);
			currentThreshold = (currentThreshold>255) ? 255 : (currentThreshold<0) ? 0 : currentThreshold; 			
			imageFound = (detector.detectMarkerLite(raster, currentThreshold) && detector.getConfidence() > 0.5) ;						
			if(imageFound)
			{ 
				detector.getTransformMatrix(result);
				container.setTransformMatrix(result);
				container.visible = true;				
				threshold = currentThreshold;
				thresholdVariance = 0; 				
				if(onImageFound!=null) onImageFound();
			} 
			else 
			{
				if(counter==2) thresholdVariance +=2; 				
				if(thresholdVariance>128 ) thresholdVariance = 1; 				
				if(onImageLost!=null) onImageLost(); 				
			}				
			singleRender(); 
		}		
		public function onImageLost():void
		{
			container.removeChild(model);
			
		}
		public function onImageFound():void{
			container.addChild(model);
					
		}
		
	}
}
